package com.rk17.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class StreamUtil {
	public static void closeAll(AutoCloseable src) throws IOException {
		try {
			src.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("all")
	public static String readTextFile(InputStream src) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(src, StandardCharsets.UTF_8));
		byte b[] = new byte[2048];
		int len = 0;
		int temp = 0;
		String stu = "";
		try {
			while ((temp = reader.read()) != -1) {
				b[len] = (byte) temp;
				len++;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				closeAll(src);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return new String(b, 0, len);
		}
	}

	@SuppressWarnings("all")
	public static String readTextFile(File txtFile) {
		String file = "";
		try {
			FileInputStream inputStream = new FileInputStream(txtFile);
			file = readTextFile(inputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			return file;
		}
	}
	
	
}
